$(function(){
	//alert("aaa");
	var subs = $('.Xdivision_Ones ul').find('li');
	
	var sum = subs.length;
	
	var arr = new Array();
	
	for(var i=0;i<sum;i++){
		
		arr[i] = new Array();
		arr[i][0] = $(subs[i]).attr('data-x');      				//x
		arr[i][1] = $(subs[i]).attr('data-y'); 						//y
		arr[i][6] = $(subs[i]).find(".Xdivision_Btn a").attr('href');                 //商铺id
		arr[i][2] = $(subs[i]).find('.Xdivision_Img img').attr('src');			//图片
		arr[i][3] = $(subs[i]).find('.Xdivision_Tit').text();					//店名
		arr[i][4] = $(subs[i]).find('.Xdivision_Adr').text();				//地址
		arr[i][5] = $(subs[i]).find('.Xdivision_Tel').text();			//电话
		//arr[i][6] = $(subs[i]).find('.wechat').text();				//微信
		arr[i][7] = $(subs[i]).find('.LoveMain').text();				//点赞数
		
		//alert(arr[i][6]);
	}

	
	window.onload=getLocation;												//距离
//	
	
	function getLocation(){
				//alert("aaa");
		var options = {
			enableHighAccuracy: true,
			maximumAge: 1000
		}
		
		if (navigator.geolocation) {
			//浏览器支持geolocation
			navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
				
		} else {
			//浏览器不支持geolocation
			alert('您的浏览器不支持该方法！')
		}
		var longitude,latitude;
		//成功时
		function onSuccess(position) {
			
			//经度
			longitude = position.coords.longitude;
			//纬度
			latitude = position.coords.latitude;
			
			var ggPoint = new BMap.Point(longitude,latitude);			
			//alert("开始经度:"+longitude+"纬度："+latitude);
			//坐标转换完之后的回调函数
			translateCallback = function (data){
			  if(data.status === 0) {
				longitude=data.points[0]['lng'];
				latitude=data.points[0]['lat'];	
				//alert("转换后经度:"+longitude+"纬度："+latitude);
				Detail(longitude,latitude);
			  }
			}			
			var convertor = new BMap.Convertor();
			var pointArr = [];
			pointArr.push(ggPoint);
			convertor.translate(pointArr, 1, 5, translateCallback)
			//alert("最后经度:"+longitude+"纬度："+latitude);
//			$.ajax({
//				type:"get",
//				url:"",
//				async:true,
//				data:{'longitude':longitude,
//					 'latitude':latitude,
//				},
//				success:function(data){
//					
//				},
//				error:function(){
//					
//				}
//			});
			//alert("经度：" + longitude + "纬度：" + latitude);

				
				
				
					
		}
		function Detail(longitude,latitude){
			//alert("经度：" + longitude + "纬度：" + latitude);
			for(var j=0;j<sum;j++){
					
					var R = 6371;
					var MLonA = longitude;
					var MLatA = 90 - latitude;
					var MLonB = arr[j][0];
					var MLatB = 90 - arr[j][1];
		
					var C = Math.sin(MLatA) * Math.sin(MLatB) * Math.cos(MLonA - MLonB) + Math.cos(MLatA) * Math.cos(MLatB);
		
					var Distance = Math.round((R * (Math.acos(C)) * Math.PI / 180) * 1000);
						
					arr[j][8] = Distance;
					//alert(Distance);
					
			}
			reBuild(arr);
		}
		
		//失败时
		function onError(error){
			switch (error.code){
				case 1:
					alert("位置服务被拒绝");
					break;

				case 2:
					alert("暂时获取不到位置信息");
					break;

				case 3:
					alert("获取信息超时");
					break;

				case 4:
					alert("未知错误");
					break;
			}
		}
	}

	function reBuild(arr){
			//alert("aaa");
		$('.Xdivision_Ones ul').empty();
		
		for(var m=0;m<sum-1;m++){
			
			for(var n=0;n<sum-m-1;n++){
				
				if(arr[n][8]>arr[n+1][8]){
					
					var temp = arr[n];
					
					arr[n] = arr[n+1];
					
					arr[n+1] = temp;
					}
			}
		}
		
	
		for(var g=0;g<sum;g++){
			
			var $str ="";
			
			var num = parseInt(arr[g][8]);
			
			if(num<1000){
				$str = num+"m";
			}else{
				num = parseFloat((num * 0.001).toFixed(3));
				$str = num+"km";
			}
			//alert($str);
			var html = "";								
			html += "<li><div class='Xdivision_Left'><div class='Xdivision_Img'><img class='lazy' src='"+arr[g][2]+"' ></div>";
			html += "<div class='Xdivision_distance'><img class='Xdistance_Inco' src='http://final.vgang.net/disk/longxia/Public/Home/images/Xdistance_Inco.png'>距离:"+$str+"</div></div>";	
			html += "<div class='Xdivision_Right'><h4 class='Xdivision_Tit'>"+arr[g][3]+"</h4>";
			html += "<div class='Xdivision_TelMain'><div class='Xdivision_AdrTit'><img class='lazy WaImg' src='http://final.vgang.net/disk/longxia/Public/Home/images/Xdivision_AdrTit.png'></div><p class='Xdivision_Adr'>"+arr[g][4]+"</p></div>";
			html += "<div class='Xdivision_AdrMain'><div class='Xdivision_TelTit'><img class='lazy WaImg' src='http://final.vgang.net/disk/longxia/Public/Home/images/Xdivision_TelTit.png'></div><p class='Xdivision_Tel'><a href='tel:"+arr[g][5]+"'>"+arr[g][5]+"</a></p></div>";
			html += "<div class='Xdivision_Love'><img class='lazy Xdistance_Inco' src='http://final.vgang.net/disk/longxia/Public/Home/images/love.png'>点赞数：<span class='LoveMain'>"+arr[g][7]+"</span></div>";
			html += "<div class='Xdivision_Btn'><a href='"+arr[g][6]+"'><img class='lazy WaImg' src='http://final.vgang.net/disk/longxia/Public/Home/images/Xdivision_Btn.png'></a></div></div></li>";
			
			
			$(html).appendTo('.Xdivision_Ones ul');
			
		}
		
		lazyload();
	}			
	function lazyload(){			
		$("img.lazy").lazyload({				
			threshold : 200				
//			    effect:"fadeIn"
    	});

	}
});
